/*
 * Copyright (C) 2022 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.arthas.psimobile.fragment.report;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;

import com.arthas.psimobile.core.BaseFragment;
import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.PercentFormatter;
import com.github.mikephil.charting.utils.MPPointF;
import com.xuexiang.psimobile.R;
import com.xuexiang.psimobile.databinding.FragmentHomePageBinding;
import com.xuexiang.psimobile.databinding.FragmentReportViewEnterpriseBinding;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xpage.enums.CoreAnim;
import com.xuexiang.xui.widget.actionbar.TitleBar;

import java.util.ArrayList;
import java.util.List;

@Page(anim = CoreAnim.none, name = "企业信用报告")
public class ReportViewEnterpriseFragment extends BaseFragment<FragmentReportViewEnterpriseBinding> implements View.OnClickListener {

    PieChart chart;

    @Override
    protected TitleBar initTitle() {
        TitleBar titleBar = super.initTitle();
        titleBar.setBackground(getResources().getDrawable(R.drawable.titlebar_blue_cube));
        return titleBar;
    }

    @Override
    public void onClick(View view) {

    }

    @NonNull
    @Override
    protected FragmentReportViewEnterpriseBinding viewBindingInflate(LayoutInflater inflater, ViewGroup container) {
        return FragmentReportViewEnterpriseBinding.inflate(inflater, container, false);
    }

    @Override
    protected void initViews() {
        chart = findViewById(R.id.credit_chart);
        initChartStyle();
        initChartLabel();

        setChartData(new Float(38.2), new Float(44.2), new Float(16.1));

        chart.animateY(1400, Easing.EaseInOutQuad);
    }

    protected void initChartStyle() {
        chart.setUsePercentValues(true);
        chart.getDescription().setEnabled(false);
        chart.setExtraOffsets(6, 10, 5, 5);

        chart.setDragDecelerationFrictionCoef(0f);

        chart.setDrawCenterText(true);
        chart.setDrawHoleEnabled(true);

        chart.setHoleRadius(10f);
        chart.setHoleColor(Color.WHITE);

        chart.setTransparentCircleColor(Color.WHITE);
        chart.setTransparentCircleAlpha(110);
        chart.setTransparentCircleRadius(10f);

        chart.setRotationAngle(180);
        chart.setRotationEnabled(false);
    }

    protected void initChartLabel() {
        chart.getLegend().setEnabled(false);
    }

    protected void setChartData(float per1, float per2, float per3) {
        List<PieEntry> entries = new ArrayList<>();
        entries.add(new PieEntry(per1, "消费者业务"));
        entries.add(new PieEntry(per2, "运营商业务"));
        entries.add(new PieEntry(per3, "企业业务"));
        entries.add(new PieEntry(100 - per1 - per2 - per3, "其他"));

        PieDataSet dataSet = new PieDataSet(entries, "Election Results");
        dataSet.setDrawIcons(false);
        dataSet.setSliceSpace(3f);
        dataSet.setIconsOffset(new MPPointF(0, 40));
        dataSet.setSelectionShift(5f);

        List<Integer> colors = new ArrayList<>();
        colors.add(Color.rgb(246, 156, 10));
        colors.add(Color.rgb(173, 219, 123));
        colors.add(Color.rgb(87, 167, 238));
        colors.add(Color.rgb(255, 242, 204));
        dataSet.setColors(colors);

        PieData data = new PieData(dataSet);
        data.setValueFormatter(new PercentFormatter(chart));
        data.setValueTextSize(11f);
        data.setValueTextColor(Color.WHITE);
        chart.setData(data);

        chart.highlightValues(null);
        chart.invalidate();
    }

    @Override
    protected void initListeners() {
        super.initListeners();
    }
}
